$(function () {
    jsHelper.init();
});
var jsHelper = {
    localData: null,
    templateList: $("#template_list").html(),
    params: {
        keyword: "",
        status: -1,
        pageIndex: 1
    },
    init: function () {
        var self = this;
        hotUtil.loading.show();
        hotUtil.ajaxCall("/userLevel/ajax/list", self.params, function (ret) {
            hotUtil.loading.close();
            if (ret.resultCode === 2000) {
                var data = ret.data;
                self.localData = data;
                var appendHtml = '';
                $.each(data, function (i, item) {
                    var tempHtml = self.templateList;
                    tempHtml = tempHtml.replace(/{index}/gm, i + 1);
                    tempHtml = tempHtml.replace(/{levelName}/gm, item.levelName);
                    var inviteRebate = '暂无';
                    if (item.inviteRebate != null) {
                        var temp = item.inviteRebate.split(',');
                        inviteRebate = '上代:' + temp[0] + '元';
                        if (temp.length > 1) {
                            inviteRebate += '，上上代：' + temp[1] + '元';
                        }
                    }
                    tempHtml = tempHtml.replace(/{inviteRebate}/gm, inviteRebate);
                    tempHtml = tempHtml.replace(/{inviteMultiRebate}/gm, jsHelper.parseInviteMultiRebate(item.inviteMultiRebate));
                    tempHtml = tempHtml.replace(/{deliveryRebate}/gm, item.deliveryRebate == null ? "0" : item.deliveryRebate);
                    tempHtml = tempHtml.replace(/{levelId}/gm, item.levelId);
                    tempHtml = tempHtml.replace(/{billAmount}/gm, item.billAmount == null ? '未设置' : item.billAmount + '元');
                    tempHtml = tempHtml.replace(/{minBuyNum}/gm, item.minBuyNum > 0 ? item.minBuyNum : 1);
                    appendHtml += tempHtml;
                });
                $("#table-content").html(appendHtml);
            }
        });
    },
    parseInviteMultiRebate: function (rebateStr) {
        var a = JSON.parse(rebateStr);

        var generationStr = [];
        for (var i = 0; i < a.priceList.length; i++) {
            if (a.priceList[i] === 0) {
                break;
            }
            generationStr.push((i + 1) + '代：' + a.priceList[i] + '元');
        }
        var debitDesc = '无';
        if (a.debitNum > 0) {
            debitDesc = a.debitNum + '件';
        }
        debitDesc = '报单：' + debitDesc;
        return debitDesc + '<br>奖励：' + generationStr.join('；');
    },
    getModel: function (dataId) {
        var model = null;
        if (this.localData != null && this.localData.length > 0) {
            $.each(this.localData, function (i, item) {
                if (item.goodsId === dataId) {
                    model = item;
                    return false;
                }
            });
        }
        return model;
    },
    edit: function (dataId) {
        window.location.href = 'edit?levelId=' + dataId;
    }
}